package com.symaster.backups.frame.element.icon;

import com.symaster.common.swing.util.SwingUtil;
import lombok.Getter;
import lombok.Setter;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.AffineTransform;

/**
 * @author yinmiao
 * @version 2024/2/26
 */
public class RotateIcon extends JComponent {

    private final Image icon;

    @Setter
    @Getter
    private float angle;
    @Setter
    @Getter
    private float angleOffset;

    public RotateIcon(Image icon) {
        this.icon = icon;
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(icon.getWidth(null), icon.getHeight(null));
    }

    @Override
    public void paint(Graphics g) {
        if (icon != null) {
            int imgW = icon.getWidth(null);
            int imgH = icon.getHeight(null);

            Graphics2D g2d = (Graphics2D) g;
            g2d.transform(AffineTransform.getRotateInstance(
                    (angle + angleOffset) * (Math.PI / 180),
                    getWidth() / 2f, getHeight() / 2f));
            g2d.drawImage(icon, Math.round(getWidth() / 2f - imgW / 2f), Math.round(getHeight() / 2f - imgH / 2f), null);
        }
    }
}
